import { reqRegister } from "../../request/api"
import { myAlert } from "../../utils/alert"

//状态
const initState ={
        user:{
            phone:"",
            password:"",
            nickname:""          
        }
}
//action types
const TYPES= {
    //修改user的type
    LOGIN_CHANGE_USER:"LOGIN_CHANGE_USER",
}
//action  creators
export const actions = {
    //修改user的action
    changeUser:(value,key)=>({type:TYPES.LOGIN_CHANGE_USER, value,key}),
    //注册
    reqRegister:(router)=>{
        return(dispatch,getState)=>{
            let{user}=getState().register;
            reqRegister(user).then(res=>{
                //判断是否成功
                if(res.data.code===200){
                    //弹成功
                    myAlert(res.data.msg)
                    //跳转页面
                    router.push("/login")
                }
            }) 
        }
    }  
}

//reducer
const reducer=(state=initState, action)=>{
    switch(action.type){
        //修改user
        case TYPES.LOGIN_CHANGE_USER:
            return{
                ...state,
                user:{
                    ...state.user,
                    [action.key]:action.value
                }
            }
        default:
            return state

    }
}

export default reducer
//selector
//用户输入值
export const getUser =state=> state.register.user;